package com.bluesky.trace;

import com.bluesky.comm.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;

/**
 * 在返回值中填充traceId，用于方便排查错误
 */
@Aspect
@Order
public class ResultTraceIdAspect {
    //"execution(* com.itsoku..*Controller.*(..)) ||execution(* com.itsoku.lesson017.web.GlobalExceptionHandler.*(..))"

    //execution(* com.bluesky..*(..)) || execution(* com.bluesky.web.GlobalExceptionHandler.*(..))
    @Pointcut("execution(* com.bluesky..*Controller.*(..)) || execution(* com.bluesky.web.GlobalExceptionHandler.*(..))")
    public void pointCut(){

    }


    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        Object object = pjp.proceed();
        if(object instanceof Result){
            ((Result<?>) object).setTraceId(TraceUtils.getTraceId());
        }
        return object;
    }
}
